<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Liebiao extends \Phpcmf\Home\Module
{

	public function index() {
		// 获取get参数
		$catid = (int)\Phpcmf\Service::L('input')->get('catid');
		$pagesize = (int)\Phpcmf\Service::L('input')->get('pagesize'); // 页数量
		$page = (int)\Phpcmf\Service::L('input')->get('page'); // 当前页
		$shuxing = (int)\Phpcmf\Service::L('input')->get('shuxing'); // 属性 0是最新，1是最热，2热评
		if($shuxing == 1){
			$order = 'hits';
		} elseif($shuxing == 2){
			$order = 'comments';
		} else {
			$order = 'inputtime desc,id desc';
		}
		if(!$pagesize){
			$pagesize = 20; // 默认值
		}
		if(!$page){
			$page = 1; // 默认第一页
		}
		if($catid){
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$daohang_0 = \Phpcmf\Service::M()->db->table('1_quan_category')->where('id', $catid)->get()->getRowArray();
			// 初始化为空数组
			$childids = [];
			// 处理childids
			if ($daohang_0['childids']) {
				$childids = explode(',', $daohang_0['childids']);
			}

			$in_id = [];
			foreach ($childids as $i) {
				$in_id[] = $i;
			}
			$shuju_data = \Phpcmf\Service::M()->table('1_quan')->where('fstatus', 1)->where_in('catid', $in_id)->order_by($order)->limit($per_page, $offset)->getAll();
		} else {
			$page_data = $page; // 从第一页开始
			$per_page = $pagesize; // 每页20条
			$offset = ($page_data - 1) * $per_page;
			$shuju_data = \Phpcmf\Service::M()->table('1_quan')->where('fstatus', 1)->order_by($order)->limit($per_page, $offset)->getAll();
		}
		
		// 循环 $shuju_data ， 把 $shuju_data['content'] 中的图片路径替换为 域名 + 图片路径
		$list_data = [];
		foreach ($shuju_data as $key => $val) {
			$quan_data = \Phpcmf\Service::M()->db->table('1_quan_data_0')->where('id', $val['id'])->get()->getRowArray();
			$daohang_data = \Phpcmf\Service::M()->db->table('1_quan_category')->where('id', $val['catid'])->get()->getRowArray();
			// 过滤pid中的0值
			$pid = $daohang_data['pid']; // 假设原始pid是标量
			if (is_array($pid)) {
			    $pid = array_filter($pid, function($value) {
			        return $value !== 0;
			    });
			    // 如果过滤后数组为空，设置默认值1
			    if (empty($pid)) {
			        $pid = [1];
			    }
			}
			$daohang_data_1 = \Phpcmf\Service::M()->db->table('1_quan_category')->where('id', $pid)->get()->getRowArray();

			// 内容图片列表
			$tupian_data = dr_get_content_img($val['neirong']);
			$tupian_list = [];
			foreach ($tupian_data as $k => $v) {
				// 去除&quot;及前面的内容
                $v = preg_replace('/.*&quot;/', '', $v);
				$tupian_list[] = dr_thumb($v, 250, 250, 0, 'crop');
				
			}
			// 头像
			if(dr_avatar($val['uid'])){
				$touxiang = dr_avatar($val['uid']);
			} else {
				$touxiang = 1;
			}
			// 问答图 
			$wendatu_list = [];
			foreach ($val['wendatu'] as $k => $v) {
				if ($k <= 8) {
					$wendatu_list[] = dr_thumb($v['file'], 250, 250, 0, 'crop');
				}
			}
			// 名称
			$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $val['uid'])->get()->getRowArray();
			$user = dr_member_info($val['uid']);
			if($userweixin){
				$mingcheng = $userweixin['nickname'];
			} else if($user['name']) {
				$mingcheng = $user['name'];
			} else {
				$mingcheng = $user['username'];
			}
			$dianzan = \Phpcmf\Service::M()->db->table('1_quan_dianzan')->where('tieid', $val['id'])->countAllResults();
			$shoucang = \Phpcmf\Service::M()->db->table('1_quan_shoucang')->where('tieid', $val['id'])->countAllResults();
			
			// 点赞用户头像
			$dianzan_data = \Phpcmf\Service::M()->table('1_quan_dianzan')->where('tieid', $val['id'])->getAll();
			$dianzan_touxiang = [];
			foreach ($dianzan_data as $k => $v) {
				$dianzan_touxiang[] = dr_avatar($v['uid']);
			}
			//计算 运营天数
    		$tianshu_0 = ( time() - $val['inputtime']) / 86400;
    		//获取$tianshu_0 的整数部分
    		$tianshu = intval($tianshu_0);
			// 帖子内容列表
			$list_data[] = [
				'id' => $val['id'],
				'cid' => $val['catid'],
				'uid' => $val['uid'],
				'title' => $val['title'],
				/*'touxiang' => $touxiang,
				'mingcheng' => $mingcheng,
				
				'tupian' => $tupian_list,
				'wendatu' => $wendatu_list,
				'jianjie' => dr_strcut(dr_clearhtml($quan_data['content']), 100),
				'fabushijian' => dr_date($val['inputtime'], 'Y-m-d H:i:s'),
				'name' => $daohang_data_1['name'].'/'.$daohang_data['name'],
				'catid' => $val['catid'],
				'pid' => $pid,
				'dianzan' => $dianzan,
				'pinglun' => $val['comments'],
				'shoucang' => $shoucang,
				'dianzan_touxiang' => $dianzan_touxiang,
				'moshi' => $val['moshi'],
				'shangjin' => $val['shangjin'],
				'zhuangtai' => $val['zhuangtai'],*/
				//数据
				'jiage' => $val['jiage'],
				'thumb' => dr_get_file($val['thumb']),
				'kaifangfabu' => $val['kaifangfabu'],
				'description' => $val['description'],
				'quantie_total' => $val['quantie_total'],
				'jiaru_total' => $val['jiaru_total'],
				'tianshu' => $tianshu, //运营天数
				'comments' => $val['comments'],
			];
		}
		$tongji = \Phpcmf\Service::M()->db->table('1_quan')->where('fstatus', 1)->countAllResults();
		$daan_tongji = \Phpcmf\Service::M()->db->table('1_quan_comment')->where('reply >', 0)->countAllResults();

		$data = [
			'list' => $list_data,
			'tongji' => $tongji,
			'in_id' => $in_id,
			'daan_tongji' => $daan_tongji,
			
		];

		$this->_json('1', '读取成功', $data);
	}

}
